package reservator.common;

import java.io.Serializable;
import java.util.Calendar;

/**
 * Represent a journey of the reservator system
 * @author Pascal Cuisinaud, Thomas Di'Meco
 */
public class Journey implements Serializable
{
    private static final long serialVersionUID = 1L;
    
    private int       id;
    private String    startPlace;
    private String    endPlace;
    private Calendar  startDate;
    private Calendar  endDate;
    private Transport transport;
    
    /**
     * Default constructor
     */
    public Journey () {}
    
    /**
     * Construct a journey with the given informations
     * @param id          The journey id
     * @param startPlace  The journey start place
     * @param endPlace    The journey end place
     * @param startDate   The journey start date
     * @param endDate     The journey end date
     * @param transport   The journey transport
     */
    public Journey (int id, String startPlace, String endPlace,
                            Calendar startDate, Calendar endDate,
                            Transport transport)
    {
        this.id = id;
        this.startDate = startDate;
        this.endDate = endDate;
        this.startPlace = startPlace;
        this.endPlace = endPlace;
        this.transport = transport;
    }
    
    /**
     * Equals method for Journey
     * @param object to be checked
     * @return true if object equals this Journey, false otherwise
     */
    @Override
    public boolean equals (Object obj)
    {
        if (this == obj) return true;
        if (!(obj instanceof Journey)) return false;
        
        Journey other = (Journey) obj;
        
        return (this.id == other.id) &&
               this.startPlace.equals(other.startPlace) &&
               this.endPlace.equals(other.endPlace) &&
               this.startDate.equals(other.startDate) &&
               this.endDate.equals(other.endDate) &&
               this.transport.equals(other.transport);
    }

    
    /**
     * ToString method
     * @return A string that contains the class intance's informations
     */   
    @Override
    public String toString ()
    {
        return "Journey [id=" + id + ", startPlace=" + startPlace
                + ", endPlace=" + endPlace + ", startDate=" + startDate.get(Calendar.DAY_OF_MONTH)+"/" + startDate.get(Calendar.MONTH)+"/"+startDate.get(Calendar.YEAR)
                + ", endDate=" + endDate.get(Calendar.DAY_OF_MONTH)+"/" + endDate.get(Calendar.MONTH)+"/"+endDate.get(Calendar.YEAR) + ", transport=" + transport + "]";
    }
    
    /**
     * Create a Journey from a String which contains journey informations.
     * (reverse of toString())
     * 
     * @param A
     *            string that contains the class intance's informations
     */
    public void createJourney (String s)
    {
        // extracting data from the string and use setters to put it in the
        // journey
        String t[] = s.split("transport=");
        setId(Integer.valueOf(t[0].split(",")[0].split("=")[1]));
        setStartPlace(t[0].split(",")[1].split("=")[1]);
        setEndPlace(t[0].split(",")[2].split("=")[1]);
        setStartDate(t[0].split(",")[3].split("=")[1]);
        setEndDate(t[0].split(",")[4].split("=")[1]);
        Transport transport = new Transport();
        transport.createTransport(t[1]);
        setTransport(transport);
    }

    // ************* Getters and Setters *************
    
    /**
     * setter of StartDate from a String at format (day/month/year)
     * @param A string that contains the calendar's informations
     */ 
	private void setStartDate(String string) {
		String result[] = string.split("/");
		startDate = Calendar.getInstance();
		startDate.set(Integer.valueOf(result[2]), Integer.valueOf(result[1]), Integer.valueOf(result[0]));
	}
	
    /**
     * setter of EndDate from a String at format (day/month/year)
     * @param A string that contains the calendar's informations
     */ 
	private void setEndDate(String string) {
		String result[] = string.split("/");
		endDate = Calendar.getInstance();
		endDate.set(Integer.valueOf(result[2]), Integer.valueOf(result[1]), Integer.valueOf(result[0]));
	}
    
    public int getId ()
    {
        return id;
    }

    public void setId (int id)
    {
        this.id = id;
    }

    public String getStartPlace ()
    {
        return startPlace;
    }

    public void setStartPlace (String startPlace)
    {
        this.startPlace = startPlace;
    }

    public String getEndPlace ()
    {
        return endPlace;
    }

    public void setEndPlace (String endPlace)
    {
        this.endPlace = endPlace;
    }

    public Calendar getStartDate ()
    {
        return startDate;
    }

    public void setStartDate (Calendar startDate)
    {
        this.startDate = startDate;
    }

    public Calendar getEndDate ()
    {
        return endDate;
    }

    public void setEndDate (Calendar endDate)
    {
        this.endDate = endDate;
    }

    public Transport getTransport ()
    {
        return transport;
    }

    public void setTransport (Transport transport)
    {
        this.transport = transport;
    }
   // **********************************************
    
   
}
